import request from '@/app/utils/request'

const baseURL = '/RAGflow'
console.log('baseURL', process.env.NEXT_PUBLIC_RAGFLOW_API_URL, baseURL)
/**
 * 创建知识库
 * curl --request POST \
     --url http://{address}/api/v1/datasets \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '{
      "name": "test_1"
      }'
 */
export const CreateDataset = (data: any, key: string) => {
    return request.post(`/api/v1/datasets`, data, {
        token: key,
        baseURL: baseURL
    })
}

/**
 * 删除知识库
 * curl --request DELETE \
     --url http://{address}/api/v1/datasets \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '{
     "ids": ["test_1", "test_2"]
     }'
 */
export const DeleteDataset = (ids: any, key: string) => {
    return request.delete(`/api/v1/datasets`, {
        data: {
            ids: ids
        },
        token: key,
        baseURL: baseURL
    })
}

/**
 * 修改知识库
 * curl --request PUT \
     --url http://{address}/api/v1/datasets/{dataset_id} \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '
     {
          "name": "updated_dataset"
     }'
 */
export const UpdateDataset = (dataset_id: string, name: string, key: string) => {
    return request.put(`/api/v1/datasets/${dataset_id}`, { name: name }, {
        token: key,
        baseURL: baseURL
    })
}

// 方法：GET
// 获取知识库
// 网址：/api/v1/datasets?page={page}&page_size={page_size}&orderby={orderby}&desc={desc}&name={dataset_name}&id={dataset_id}
// 头：'Authorization: Bearer <YOUR_API_KEY>'
// page：（Filter 参数）指定将显示数据集的页面。默认为 。1
// page_size：（Filter 参数）每页上的数据集数。默认为 。30
// orderby：（Filter 参数）应作为数据集排序依据的字段。可用选项：create_time(默认)  update_time
// desc：（Filter 参数）指示是否应将检索到的数据集按降序排序。默认为 。true
// name：（Filter 参数）要检索的数据集的名称。
// id：（Filter 参数）要检索的数据集的 ID。
// 获取知识库
export const GetDatasets = (page: number, page_size: number, orderby: string, desc: boolean, dataset_name: string, dataset_id: string, key: string) => {
    return request.get(`/api/v1/datasets?page=${page}&page_size=${page_size}&orderby=${orderby}&desc=${desc}&name=${dataset_name}&id=${dataset_id}`, {
        token: key,
        baseURL: baseURL
    })
}


/**
 * 获取文档列表
dataset_id：（路径参数）关联的数据集 ID。
keywords：（Filter 参数），用于匹配文档标题的关键字。string
page：（Filter 参数），指定将显示文档的页面。默认为 。integer1
page_size：（Filter 参数），每页上的最大文档数。默认为 。integer30
orderby：（Filter 参数），文档应作为其排序依据的字段。可用选项：string  create_time（默认） update_time
desc：（Filter 参数），指示是否应按降序对检索到的文档进行排序。默认为 。booleantrue
id：（Filter 参数），要检索的文档的 ID。string
curl --request GET \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents?page={page}&page_size={page_size}&orderby={orderby}&desc={desc}&keywords={keywords}&id={document_id}&name={document_name} \
     --header 'Authorization: Bearer <YOUR_API_KEY>'
 */
export const GetWord = (dataset_id: string, page: number, page_size: number, keywords: string, id: string, key: string) => {
    return request.get(`/api/v1/datasets/${dataset_id}/documents?page=${page}&page_size=${page_size}&orderby=${'create_time'}&desc=${true}&keywords=${keywords}&id=${id} `, {
        token: key,
        baseURL: baseURL
    })
}


/**
 * 删除文档
dataset_id：（路径参数）关联的数据集 ID。
ids：（Body 参数），要删除的文档的 ID。如果未指定，则将删除指定数据集中的所有文档。list[string]
curl --request DELETE \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '
     {
          "ids": ["id_1","id_2"]
     }'
 */
export const DeleteWord = (dataset_id: string, ids: string[], key: string) => {
    return request.delete(`/api/v1/datasets/${dataset_id}/documents`, {
        token: key,
        baseURL: baseURL,
        data: {
            ids: ids
        }
    })
}
/**
 * 修改文档
 * curl --request PUT \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents/{document_id} \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --header 'Content-Type: application/json' \
     --data '
     {
          "name": "manual.txt", 
          "chunk_method": "manual", 
          "parser_config": {"chunk_token_count": 128}
     }'

 */
export const UpdateWord = (dataset_id: string, document_id: string, data: any, key: string) => {
    return request.put(`/api/v1/datasets/${dataset_id}/documents/${document_id}`, data, {
        token: key,
        baseURL: baseURL
    })
}
/**
 * 解析文档
dataset_id：（路径参数）数据集 ID。
document_ids： （Body 参数）， ， 必填要解析的文档的 ID。list[string]
curl --request POST \
     --url http://{address}/api/v1/datasets/{dataset_id}/chunks \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '
     {
          "document_ids": ["97a5f1c2759811efaa500242ac120004","97ad64b6759811ef9fc30242ac120004"]
     }'
 */

export const ParseWord = (dataset_id: string, document_ids: string[], key: string) => {
    return request.post(`/api/v1/datasets/${dataset_id}/chunks`, { document_ids: document_ids }, {
        token: key,
        baseURL: baseURL,
    })
}

/**
 * 停止解析文档
 * curl --request DELETE \
     --url http://{address}/api/v1/datasets/{dataset_id}/chunks \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '
     {
          "document_ids": ["97a5f1c2759811efaa500242ac120004","97ad64b6759811ef9fc30242ac120004"]
     }'
 */
export const StopParseWord = (dataset_id: string, document_ids: string[], key: string) => {
    return request.delete(`/api/v1/datasets/${dataset_id}/chunks`, {
        token: key,
        baseURL: baseURL,
        data: {
            document_ids: document_ids
        }
    })
}

/**
 * 下载文档
 * curl --request GET \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents/{document_id} \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --output ./ragflow.txt
 */
export const DownloadWord = (dataset_id: string, document_id: string, key: string) => {
    return request.get(`/api/v1/datasets/${dataset_id}/documents/${document_id}`, {
        token: key,
        baseURL: baseURL,
        responseType: 'blob'
    })
}

/**
 * curl --request POST \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents \
     --header 'Content-Type: multipart/form-data' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --form 'file=@./test1.txt' \
     --form 'file=@./test2.pdf'
 */

export const UploadWord = (dataset_id: string, file: any, key: string) => {
    return request.post(`/api/v1/datasets/${dataset_id}/documents`, file, {
        token: key,
        baseURL: baseURL,
        headers: { 'Content-Type': 'multipart/form-data' }
    })
}

/**
 * 列出块
 * curl --request GET \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents/{document_id}/chunks?keywords={keywords}&page={page}&page_size={page_size}&id={chunk_id} \
     --header 'Authorization: Bearer <YOUR_API_KEY>' 
 */
export const ListChunk = (dataset_id: string, document_id: string, keywords: string, page: number, page_size: number, chunk_id: string, key: string) => {
    return request.get(`/api/v1/datasets/${dataset_id}/documents/${document_id}/chunks?keywords=${keywords}&page=${page}&page_size=${page_size}&id=${chunk_id}`, {
        token: key,
        baseURL: baseURL
    })
}

/**
 * 添加块
 * curl --request POST \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents/{document_id}/chunks \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '
     {
          "content": "<CHUNK_CONTENT_HERE>"
     }'
 */
export const AddChunk = (dataset_id: string, document_id: string, data: any, key: string) => {
    return request.post(`/api/v1/datasets/${dataset_id}/documents/${document_id}/chunks`, data, {
        token: key,
        baseURL: baseURL
    })
}


/**
 * 删除块
 * curl --request DELETE \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents/{document_id}/chunks \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '
     {
          "chunk_ids": ["test_1", "test_2"]
     }'
 */
export const DeleteChunk = (dataset_id: string, document_id: string, chunk_ids: string[], key: string) => {
    return request.delete(`/api/v1/datasets/${dataset_id}/documents/${document_id}/chunks`, {
        token: key,
        baseURL: baseURL,
        data: {
            chunk_ids: chunk_ids
        }
    })
}

/**
 * 编辑块
 * curl --request PUT \
     --url http://{address}/api/v1/datasets/{dataset_id}/documents/{document_id}/chunks/{chunk_id} \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Bearer <YOUR_API_KEY>' \
     --data '
     {   
          "content": "ragflow123",  
          "important_keywords": []  
     }'
 */
export const EditChunk = (dataset_id: string, document_id: string, chunk_id: string, data: any, key: string) => {
    return request.put(`/api/v1/datasets/${dataset_id}/documents/${document_id}/chunks/${chunk_id}`, data, {
        token: key,
        baseURL: baseURL
    })
}



// 外链到dify
export const LinkWord = (data: any, key: string) => {
    return request.post('', data, {
        token: key,
        baseURL: '/retrieval'
    })
}



/**
 * curl --location --request GET 'http://10.10.10.2:5001/v1/messages/{message_id}/suggested?user=abc-123 \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
--header 'Content-Type: application/json'
 */
export const GetMessage = (message_id: string, user: string, key: string) => {
    return request.get(`/v1/messages/${message_id}/suggested?user=${user}`, {
        token: key,
        baseURL: '/retrieval'
    })
}

